<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">物料编码</label>
        <el-input v-model.trim="query.productNo" clearable placeholder="物料编码" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" @clear="crud.toQuery" />
        <label class="el-form-item-label">客户物料编号</label>
        <el-input v-model.trim="query.partnerProductNo" clearable placeholder="客户物料编号" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" @clear="crud.toQuery" />
        <label class="el-form-item-label">客户</label>
        <el-input v-model.trim="query.receiverName" clearable placeholder="客户" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" @clear="crud.toQuery" />
        <label class="el-form-item-label">物料类型</label>
        <el-select v-model="query.materialType" class="filter-item" placeholder="请选择" style="width: 185px;" @change="crud.toQuery">
          <el-option
            v-for="item in materialTypeList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <label class="el-form-item-label">物料描述</label>
        <el-input v-model="query.productDescription" clearable placeholder="物料描述" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" @clear="crud.toQuery" />
        <label class="el-form-item-label">最大绑定箱数</label>
        <el-input v-model.trim="query.maxBoxesQty" clearable placeholder="最大绑定箱数" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" @clear="crud.toQuery" />
        <label class="el-form-item-label">装运方式</label>
        <el-select v-model="query.shipmentType" class="filter-item" placeholder="请选择" style="width: 185px;" @change="crud.toQuery">
          <el-option
            v-for="item in dict.shipment_type"
            :key="item.id"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <label class="el-form-item-label">是否打带</label>
        <!-- <el-input v-model="query.createTime" clearable placeholder="是否打带" style="width: 130px;" class="filter-item" @keyup.enter.native="crud.toQuery" /> -->
        <el-select v-model="query.bandingForInbound" class="filter-item" placeholder="请选择" style="width: 185px;" @change="crud.toQuery">
          <el-option
            v-for="item in dict.band_status"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <label class="el-form-item-label">贴标</label>
        <el-select v-model="query.labelingForOutbound" class="filter-item" placeholder="请选择" style="width: 185px;" @change="crud.toQuery">
          <el-option
            v-for="item in dict.tag_status"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <label class="el-form-item-label">裹膜</label>
        <el-select v-model="query.wrappingForOutbound" class="filter-item" placeholder="请选择" style="width: 185px;" @change="crud.toQuery">
          <el-option
            v-for="item in dict.warp_status"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <label class="el-form-item-label">审核状态</label>
        <el-select v-model="query.status" class="filter-item" placeholder="请选择" style="width: 185px;" @change="crud.toQuery">
          <el-option
            v-for="item in dict.audit_status"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <span>
          <el-button type="success" size="mini" class="filter-item" icon="el-icon-search" @click="crud.toQuery">搜索</el-button>
          <el-button type="warning" size="mini" class="filter-item" icon="el-icon-refresh" @click="crud.resetQuery()">重置</el-button>
        </span>
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission">
        <el-button
          slot="right"
          type="primary"
          size="mini"
          class="filter-item"
          icon="el-icon-download"
          :loading="btn.download"
          plain
          @click="downloadExcel"
        >物料模板下载</el-button>
        <el-button
          slot="right"
          type="info"
          size="mini"
          class="filter-item"
          icon="el-icon-download"
          :loading="btn.containerDownload"
          plain
          @click="containerDownloadExcel"
        >容器模板下载</el-button>
        <el-upload
          ref="uploadRef"
          slot="right"
          class="filter-item"
          :headers="headers"
          :action="uploadUrl"
          :limit="1"
          :show-file-list="false"
          :http-request="customUpload"
        >
          <el-button
            type="success"
            size="mini"
            :loading="btn.uploadMaterial"
            icon="el-icon-upload2"
            plain
            @click="handleUpload"
          >上传物料表</el-button>
        </el-upload>
        <el-upload
          ref="uploadRef2"
          slot="right"
          class="filter-item"
          :headers="headers"
          :action="uploadUrl2"
          :limit="1"
          :show-file-list="false"
          :http-request="customUpload2"
        >
          <el-button
            type="warning"
            size="mini"
            :loading="btn.uploadContainer"
            icon="el-icon-upload2"
            plain
            @click="handleUpload2"
          >上传容器维护表</el-button>
        </el-upload>
        <el-button
          slot="right"
          type="danger"
          class="filter-item"
          plain
          size="mini"
          :disabled="crud.selections.length === 0"
          @click="moreAudit"
        >批量审核</el-button>
      </crudOperation>
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" title="新增物料" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small">
          <el-form-item label="物料编号" prop="productNo" :label-width="formLabelWidth">
            <el-input v-model="form.productNo" style="width: 280px;" />
          </el-form-item>
          <el-form-item label="物料类型" prop="materialType" :label-width="formLabelWidth">
            <el-select v-model="form.materialType" placeholder="请选择" style="width: 280px;">
              <el-option
                v-for="item in materialTypeList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="物料描述" :label-width="formLabelWidth">
            <el-input v-model="form.productDescription" style="width: 280px;" />
          </el-form-item>
          <el-form-item label="客户物料编号" prop="partnerProductNo" :label-width="formLabelWidth">
            <el-input v-model="form.partnerProductNo" style="width: 280px;" />
          </el-form-item>
          <el-form-item label="最大绑定箱数" prop="maxBoxesQty" :label-width="formLabelWidth">
            <el-input v-model.number="form.maxBoxesQty" style="width: 280px;" />
          </el-form-item>
          <el-form-item label="数量" prop="qty" :label-width="formLabelWidth">
            <el-input v-model.number="form.qty" style="width: 280px;" />
          </el-form-item>
          <el-form-item label="是否抽检物料" prop="isSpotCheck" :label-width="formLabelWidth">
            <!-- <el-input v-model.number="form.isSpotCheck" style="width: 280px;" /> -->
            <el-select v-model="form.isSpotCheck" placeholder="请选择" style="width: 280px;">
              <el-option
                v-for="item in isSpotCheckList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="装运方式" prop="shipmentType" :label-width="formLabelWidth">
            <el-select v-model="form.shipmentType" filterable placeholder="请选择" style="width: 280px;">
              <el-option
                v-for="item in dict.shipment_type"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="单位" prop="unit" :label-width="formLabelWidth">
            <el-select v-model="form.unit" filterable placeholder="请选择" style="width: 280px;">
              <el-option
                v-for="item in dict.unit_type"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="是否打带" prop="bandingForInbound" :label-width="formLabelWidth">
            <el-select v-model="form.bandingForInbound" filterable placeholder="请选择" style="width: 280px;">
              <el-option
                v-for="item in dict.band_status"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item v-if="form.bandingForInbound === '1'" label="打带方式" prop="bandingTpye" :label-width="formLabelWidth">
            <el-select v-model="form.bandingType" filterable placeholder="请选择" style="width: 280px;">
              <el-option
                v-for="item in dict.banding_type"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="出库是否贴标" prop="labelingForOutbound" :label-width="formLabelWidth">
            <el-select v-model="form.labelingForOutbound" filterable placeholder="请选择" style="width: 280px;">
              <el-option
                v-for="item in dict.tag_status"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="出库是否裹膜" prop="wrappingForOutbound" :label-width="formLabelWidth">
            <el-select v-model="form.wrappingForOutbound" filterable placeholder="请选择" style="width: 280px;">
              <el-option
                v-for="item in dict.warp_status"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item v-if="form.wrappingForOutbound === '1'" label="裹膜圈数" prop="wrappingQty" :label-width="formLabelWidth">
            <el-input v-model.number="form.wrappingQty" style="width: 280px;" />
          </el-form-item>
          <el-form-item label="客户" prop="receiverName" :label-width="formLabelWidth">
            <el-input v-model="form.receiverName" style="width: 280px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--编辑表单组件-->
      <el-dialog title="编辑物料" width="500px" :visible.sync="editDialog" :close-on-click-modal="false">
        <el-form ref="editForm" :model="editForm" :rules="editForm.materialType === 1 ? rules : rules2" size="small">
          <el-form-item label="物料编号" prop="productNo" :label-width="formLabelWidth">
            <el-input v-model="editForm.productNo" disabled style="width: 280px;" />
          </el-form-item>
          <el-form-item label="物料类型" prop="materialType" :label-width="formLabelWidth">
            <!-- <el-input v-model="editForm.materialType" disabled style="width: 280px;" /> -->
            <el-select v-model="editForm.materialType" placeholder="请选择" disabled style="width: 280px;">
              <el-option
                v-for="item in materialTypeList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="物料描述" :label-width="formLabelWidth">
            <el-input v-model="editForm.productDescription" style="width: 280px;" />
          </el-form-item>
          <el-form-item label="是否抽检物料" prop="isSpotCheck" :label-width="formLabelWidth">
            <el-select v-model="editForm.isSpotCheck" placeholder="请选择" style="width: 280px;">
              <el-option
                v-for="item in isSpotCheckList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item v-if="editForm.materialType === 1" label="客户物料编号" prop="partnerProductNo" :label-width="formLabelWidth">
            <el-input v-model="editForm.partnerProductNo" disabled style="width: 280px;" />
          </el-form-item>
          <el-form-item v-if="editForm.materialType === 1" label="最大绑定箱数" prop="maxBoxesQty" :label-width="formLabelWidth">
            <el-input v-model.number="editForm.maxBoxesQty" style="width: 280px;" />
          </el-form-item>
          <el-form-item label="数量" prop="qty" :label-width="formLabelWidth">
            <el-input v-model.number="editForm.qty" style="width: 280px;" />
          </el-form-item>
          <el-form-item v-if="editForm.materialType === 1" label="装运方式" prop="shipmentType" :label-width="formLabelWidth">
            <el-select v-model="editForm.shipmentType" filterable placeholder="请选择" style="width: 280px;">
              <el-option
                v-for="item in dict.shipment_type"
                :key="item.id"
                :label="item.label"
                :value="Number(item.value)"
              />
            </el-select>
          </el-form-item>
          <el-form-item v-if="editForm.materialType === 1" label="单位" prop="unit" :label-width="formLabelWidth">
            <!-- <el-input v-model="form.unit" style="width: 280px;" /> -->
            <el-select v-model="editForm.unit" filterable placeholder="请选择" style="width: 280px;">
              <el-option
                v-for="item in dict.unit_type"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item v-if="editForm.materialType === 1" label="是否打带" prop="bandingForInbound" :label-width="formLabelWidth">
            <el-select v-model="editForm.bandingForInbound" filterable placeholder="请选择" style="width: 280px;">
              <el-option
                v-for="item in dict.band_status"
                :key="item.id"
                :label="item.label"
                :value="Number(item.value)"
              />
            </el-select>
          </el-form-item>
          <el-form-item v-if="editForm.bandingForInbound === 1 && editForm.materialType === 1" label="打带方式" prop="bandingTpye" :label-width="formLabelWidth">
            <el-select v-model="editForm.bandingType" filterable placeholder="请选择" style="width: 280px;">
              <el-option
                v-for="item in dict.banding_type"
                :key="item.id"
                :label="item.label"
                :value="Number(item.value)"
              />
            </el-select>
          </el-form-item>
          <el-form-item v-if="editForm.materialType === 1" label="出库是否贴标" prop="labelingForOutbound" :label-width="formLabelWidth">
            <el-select v-model="editForm.labelingForOutbound" filterable placeholder="请选择" style="width: 280px;">
              <el-option
                v-for="item in dict.tag_status"
                :key="item.id"
                :label="item.label"
                :value="Number(item.value)"
              />
            </el-select>
          </el-form-item>
          <el-form-item v-if="editForm.materialType === 1" label="出库是否裹膜" prop="wrappingForOutbound" :label-width="formLabelWidth">
            <el-select v-model="editForm.wrappingForOutbound" filterable placeholder="请选择" style="width: 280px;">
              <el-option
                v-for="item in dict.warp_status"
                :key="item.id"
                :label="item.label"
                :value="Number(item.value)"
              />
            </el-select>
          </el-form-item>
          <el-form-item v-if="editForm.wrappingForOutbound === 1 && editForm.materialType === 1" label="裹膜圈数" prop="wrappingQty" :label-width="formLabelWidth">
            <el-input v-model.number="editForm.wrappingQty" style="width: 280px;" />
          </el-form-item>
          <el-form-item label="客户" prop="receiverName" :label-width="formLabelWidth">
            <el-input v-model="editForm.receiverName" style="width: 280px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="editDialog = false">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="submitEdit">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" :border="false" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="productNo" label="物料编号" />
        <el-table-column prop="materialType" label="物料类型">
          <template slot-scope="scope">
            {{ scope.row.materialType === 1 ? '成品' : '容器' }}
          </template>
        </el-table-column>
        <el-table-column prop="productDescription" label="物料描述" />
        <el-table-column prop="partnerProductNo" label="客户物料编号" />
        <el-table-column prop="receiverName" label="客户" />
        <el-table-column prop="isSpotCheck" label="是否抽检物料">
          <template slot-scope="scope">
            {{ scope.row.isSpotCheck === 1 ? '待检物料' : '否' }}
          </template>
        </el-table-column>
        <el-table-column prop="maxBoxesQty" label="最大绑定箱数" />
        <el-table-column prop="shipmentType" label="装运方式">
          <template slot-scope="scope">
            {{ dict.label.shipment_type[scope.row.shipmentType] }}
          </template>
        </el-table-column>
        <el-table-column prop="qty" label="数量" />
        <el-table-column prop="unit" label="单位" />
        <el-table-column label="入库">
          <el-table-column prop="bandingForInbound" label="是否打带">
            <template slot-scope="scope">
              {{ dict.label.band_status[scope.row.bandingForInbound] }}
            </template>
          </el-table-column>
          <el-table-column prop="bandingType" label="入库打带方式">
            <template slot-scope="scope">
              {{ dict.label.banding_type[scope.row.bandingType] }}
            </template>
          </el-table-column>
        </el-table-column>
        <el-table-column label="出库">
          <el-table-column prop="labelingForOutbound" label="是否贴标签">
            <template slot-scope="scope">
              {{ dict.label.tag_status[scope.row.labelingForOutbound] }}
            </template>
          </el-table-column>
          <el-table-column prop="wrappingForOutbound" label="是否裹膜">
            <template slot-scope="scope">
              {{ dict.label.warp_status[scope.row.wrappingForOutbound] }}
            </template>
          </el-table-column>
          <el-table-column prop="wrappingQty" label="裹膜圈数" />
        </el-table-column>
        <el-table-column prop="status" label="审核状态">
          <template slot-scope="scope">
            <!-- {{ dict.label.audit_status[scope.row.status] }} -->
            <el-tag v-if="scope.row.status === 0" type="primary">{{ dict.label.audit_status[scope.row.status] }}</el-tag>
            <el-tag v-else-if="scope.row.status === 1" type="success">{{ dict.label.audit_status[scope.row.status] }}</el-tag>
            <el-tag v-else type="danger">{{ dict.label.audit_status[scope.row.status] }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="reviewer" label="审核人" />
        <el-table-column label="操作" width="150px" align="center">
          <!-- <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template> -->
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="handlerEdit(scope.row)"
            />
            <el-button v-permission="['admin','productMaintenance:audit']" :disabled="scope.row.status !== 0 || crud.selections.length !== 0" type="danger" size="mini" @click="judgeOpen(scope.row.id)">审核</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
      <!-- 选择表单 -->
      <el-dialog
        title="审核结果选择"
        :visible.sync="selectVisible"
        width="500px"
        :before-close="selectClose"
      >
        <span style="width: 100%;display: flex;gap: 40px;">
          <el-button style="width: 50%;" :loading="btn.judge === 1" :disabled="btn.judge === 2" type="success" @click="judgeValidate(1)">审核通过</el-button>
          <el-button style="width: 50%;" :loading="btn.judge === 2" :disabled="btn.judge === 1" type="primary" @click="judgeValidate(2)">审核不通过</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getToken } from '@/utils/auth'
import crudMaterialMaintenance from '@/api/work/materialMaintenance'
import { judgeAPI, moreJudgeAPI, uploadContainerFileAPI } from '@/api/work/materialMaintenance'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import crudOperation from '@crud/CRUD.operation'
import pagination from '@crud/Pagination'

const defaultForm = { id: null, serialNo: null, productNo: null, materialType: null, productDescription: null, partnerProductNo: null, destination: null, maxBoxesQty: null, qty: null, shipmentType: null, unit: null, bandingForInbound: null, longSideBandingQty: null, shortSideBandingQty: null, wrappingQty: null, receiverName: null, labelingForOutbound: null, wrappingForOutbound: null, bandingType: null, wrappingStyle: null, status: null, createTime: null, updateTime: null }
export default {
  name: 'ProductMaintenance',
  components: { pagination, crudOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: 'material_maintenance', url: 'api/materialMaintenance', idField: 'id', sort: 'id,desc', crudMethod: { ...crudMaterialMaintenance }, optShow: {
      add: true,
      edit: true,
      del: true,
      download: true,
      reset: true
    }})
  },
  dicts: ['band_status', 'tag_status', 'warp_status', 'audit_status', 'shipment_type', 'unit_type', 'banding_type'],
  data() {
    return {
      editDialog: false,
      selectVisible: false,
      btn: {
        judge: '',
        download: false,
        containerDownload: false,
        uploadMaterial: false,
        uploadContainer: false
      },
      selectId: '',
      editForm: {},
      ids: [],
      permission: {
        add: ['admin', 'materialMaintenance:add', 'otherUser'],
        edit: ['admin', 'materialMaintenance:edit', 'otherUser'],
        del: ['admin', 'materialMaintenance:del', 'otherUser'],
        export: ['materialMaintenance:export']
      },
      formLabelWidth: '120px',
      uploadUrl: process.env.VUE_APP_BASE_API + 'api/materialMaintenance/importExcel',
      uploadUrl2: process.env.VUE_APP_BASE_API + 'api/materialMaintenance/importContainerExcel',
      headers: {},
      rules: {
        productNo: [
          { required: true, message: '物料编号不能为空', trigger: 'blur' }
        ],
        materialType: [
          { required: true, message: '物料类型不能为空', trigger: 'change' }
        ],
        maxBoxesQty: [
          { type: 'number', message: '最大绑定箱数必须为数字', trigger: 'blur' },
          { required: true, message: '最大绑定箱数不能为空', trigger: 'blur' }
        ],
        shipmentType: [
          { required: true, message: '装运方式不能为空', trigger: 'blur' }
        ],
        qty: [
          { type: 'number', message: '数量必须为数字', trigger: 'blur' },
          { required: true, message: '数量不能为空', trigger: 'blur' }
        ],
        unit: [
          { required: true, message: '单位不能为空', trigger: 'change' }
        ],
        bandingForInbound: [
          { required: true, message: '是否需要打带不能为空', trigger: 'change' }
        ],
        longSideBandingQty: [
          { required: true, message: '打带长边数量不能为空', trigger: 'blur' }
        ],
        shortSideBandingQty: [
          { required: true, message: '打带短边数量不能为空', trigger: 'blur' }
        ],
        labelingForOutbound: [
          { required: true, message: '出库是否贴标不能为空', trigger: 'change' }
        ],
        wrappingForOutbound: [
          { required: true, message: '出库是否裹膜不能为空', trigger: 'change' }
        ],
        isSpotCheck: [
          { required: true, message: '是否抽检物料不能为空', trigger: 'change' }
        ]
      },
      rules2: {
        productNo: [
          { required: true, message: '物料编号不能为空', trigger: 'blur' }
        ],
        materialType: [
          { required: true, message: '物料类型不能为空', trigger: 'change' }
        ],
        qty: [
          { type: 'number', message: '数量必须为数字', trigger: 'blur' },
          { required: true, message: '数量不能为空', trigger: 'blur' }
        ],
        isSpotCheck: [
          { required: true, message: '是否抽检物料不能为空', trigger: 'change' }
        ]
      },
      materialTypeList: [
        {
          label: '成品',
          value: 1
        },
        {
          label: '容器',
          value: 2
        }
      ],
      isSpotCheckList: [
        {
          label: '待检物料',
          value: 1
        },
        {
          label: '否',
          value: 0
        }
      ]
    }
  },
  mounted() {
    this.getInfomation()
  },
  methods: {
    moreAudit() {
      this.selectVisible = true
      this.ids = this.crud.selections.map(item => item.id)
    },
    judgeOpen(id) {
      this.selectId = id
      this.selectVisible = true
    },
    selectClose() {
      this.selectVisible = false
      this.selectId = ''
    },
    async judgeValidate(val) {
      if (this.ids.length > 0) {
        try {
          if (val === 1) {
            this.btn.judge = 1
          } else {
            this.btn.judge = 2
          }
          const result = await moreJudgeAPI({
            ids: this.ids,
            isAudit: val
          })
          if (result.result === 'OK') {
            this.$message.success('操作成功')
            this.crud.toQuery()
            this.selectVisible = false
            this.ids = []
          } else {
            return this.$message.warning(result.data)
          }
        } finally {
          this.btn.judge = ''
        }
      } else {
        try {
          if (val === 1) {
            this.btn.judge = 1
          } else {
            this.btn.judge = 2
          }
          const res = await judgeAPI({
            id: this.selectId,
            isAudit: val
          })
          if (res.result === 'OK') {
            this.$message.success('操作成功')
            this.crud.toQuery()
            this.selectVisible = false
            this.selectId = ''
          } else {
            return this.$message.warning(res.data)
          }
        } finally {
          this.btn.judge = ''
        }
      }
    },
    // 获取请求头信息
    getInfomation() {
      const token = getToken()
      if (token) {
        this.headers = {
          'Authorization': token,
          'Content-Type': 'multipart/form-data'
        }
      }
    },
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    // 发送下载请求
    async downloadExcel() {
      try {
        this.btn.download = true
        const res = await crudMaterialMaintenance.downloadFileAPI()
        if (res && res instanceof Blob) {
          this.downloadFile(res, 'material')
        } else {
          this.message.warning('文件下载失败，返回数据不是有效的文件内容')
        }
      } finally {
        this.btn.download = false
      }
    },
    async containerDownloadExcel() {
      try {
        this.btn.containerDownload = true
        const res = await crudMaterialMaintenance.containerDownloadFileAPI()
        if (res && res instanceof Blob) {
          this.downloadFile(res, 'container')
        } else {
          this.message.warning('文件下载失败，返回数据不是有效的文件内容')
        }
      } finally {
        this.btn.containerDownload = false
      }
    },
    // 解析excel文件
    downloadFile(obj, name) {
      const url = window.URL.createObjectURL(new Blob([obj]))
      const link = document.createElement('a')
      link.style.display = 'none'
      link.href = url
      let fileName
      if (name === 'material') {
        fileName = `物料维护信息表_${new Date().getTime()}.xlsx`
      } else if (name === 'container') {
        fileName = `容器维护信息表_${new Date().getTime()}.xlsx`
      }
      link.setAttribute('download', fileName)
      document.body.appendChild(link)
      link.click()
      document.body.removeChild(link)
      window.URL.revokeObjectURL(url)
    },
    // 自定义上传
    async customUpload(file) {
      const formData = new FormData()
      formData.append('file', file.file)
      try {
        this.btn.uploadMaterial = true
        const res = await crudMaterialMaintenance.uploadFileAPI(formData)
        if (res.result === 'OK') {
          switch (res.data.errorMessages.length) {
            case 0:
              this.$message.success('上传物料成功')
              break
            case 1:
              this.$message.warning('上传失败的物料编号为：' + res.data.errorList.join(',') + '。失败原因：' + res.data.errorMessages[0])
              break
            default:
              this.$message.warning('上传失败的物料编号为：' + res.data.errorList.join(',') + '。失败原因：' + res.data.errorMessages.join('或'))
              break
          }
        } else {
          this.$message.warning(res.data)
        }
      } finally {
        this.btn.uploadMaterial = false
      }
    },
    async customUpload2(file) {
      const formData = new FormData()
      formData.append('file', file.file)
      try {
        this.btn.uploadContainer = true
        const res = await uploadContainerFileAPI(formData)
        if (res.result === 'OK') {
          this.$message.success('上传成功')
        } else {
          this.$message.warning(res.data)
        }
      } finally {
        this.btn.uploadContainer = false
      }
    },
    // 清除已选择的文件
    handleUpload() {
      this.$refs.uploadRef.clearFiles() // 清除已选择的文件
      this.$refs.uploadRef.submit() // 重新提交上传
    },
    handleUpload2() {
      this.$refs.uploadRef2.clearFiles() // 清除已选择的文件
      this.$refs.uploadRef2.submit() // 重新提交上传
    },
    handlerEdit(row) {
      this.editForm = JSON.parse(JSON.stringify(row))
      this.editDialog = true
    },
    submitEdit() {
      this.$refs.editForm.validate(async(valid) => {
        if (!valid) {
          return
        }
        const res = await crudMaterialMaintenance.edit(this.editForm)
        console.log(res)
        if (res.result === 'OK') {
          this.$message.success('修改成功')
          this.editDialog = false
          this.crud.toQuery()
        } else {
          this.$message.warning(res.message)
          this.editDialog = false
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
